<?php

declare(strict_types=1);

namespace App\Controller;

use App\Model\User;
use App\Service\Hupai;
use App\Service\PaiXing;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\AutoController;

/**
 * @AutoController()
 */
class UserController extends AbstractController
{
    // Hyperf 会自动为此方法生成一个 /index/index 的路由，允许通过 GET 或 POST 方式请求
    public function index(RequestInterface $request)
    {
        // 从请求中获得 id 参数
        $id = $request->input('id', 1);
        return (string)$id;
    }

    public function info(RequestInterface $request)
    {
        // 从请求中获得 id 参数
        $token = $request->input('token');
        if (empty($token)) {
            return ERROR('请输入token');
        }
        $uinfo = User::getUserInfoByUsername($token);
        if (empty($uinfo)) {
            return ERROR('该用户不存在');
        }
        return SUCC($uinfo);
    }

    public function tt(RequestInterface $request)
    {
        $hand = [11, 14, 2, 7, 8, 14, 15, 22, 4, 5, 22, 26, 26, 21, 6, 12, 9, 6, 5, 23, 23, 14, 13, 26, 4, 11, 29, 18, 11, 21,
            7, 8, 19, 24, 6, 1, 26, 28, 27, 2, 22, 15, 21, 28, 4, 13, 6, 15, 17, 27, 27, 12, 16, 12, 28];
        $rb1 = [2, 3, 4, 5, 9, 13, 15, 17, 18, 18, 23, 25, 29];
        $rb2 = [1, 1, 2, 8, 9, 9, 16, 17, 19, 19, 24, 24, 27];
        $rb3 = [3, 5, 8, 11, 13, 16, 18, 22, 23, 24, 25, 25, 28, 29];
        $cards_pool = array_merge($hand, $rb1, $rb2, $rb3);
//        dd('cards_pool', $cards_pool);
        $res = getRandomTingPaiCards($cards_pool);
        $card1 = $res['cards'];
        $rest_cards1 = $res['rest_cards'];
        $res = getRandomTingPaiCards($res['rest_cards']);
        $card2 = $res['cards'];
        $rest_cards2 = $res['rest_cards'];
        $res = getRandomTingPaiCards($res['rest_cards']);
        $card3 = $res['cards'];
        $rest_cards3 = $res['rest_cards'];
        return [
            'card1' => $card1,
            'rest_cards1' => $rest_cards1,
            'card2' => $card2,
            'rest_cards2' => $rest_cards2,
            'card3' => $card3,
            'rest_cards3' => $rest_cards3,
        ];
    }


    public function hu(RequestInterface $request)
    {
        $index = $request->input('index', '');
        if ($index == '') {
            exit;
        }

        //定缺 1.万  2.筒  3.条
        $arr = [
            0 => [
                'color' => 2,  // 平胡
                'peng' => [],
                'gang' => [],
                'cards' => [1, 2, 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7],
            ],
            1 => [
                'color' => 2,//平胡，带4对，组成的2顺子和将牌
                'peng' => [],
                'gang' => [],//组合类型：[2,2,2,2,3,4]
                'cards' => [1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6],
            ],

            2 => [
                'color' => 2,//清一色七对
                'peng' => [],
                'gang' => [],//组合类型：[2,2,2,2,2,2,2]
                'cards' => [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7],
            ],
            3 => [
                'color' => 2, //清一色龙七对
                'peng' => [],
                'gang' => [],//组合类型：[4,4,4,2]
                'cards' => [1, 1, 1, 1, 4, 4, 4, 4, 5, 5, 5, 5, 7, 7],
            ],
            4 => [
                'color' => 2,//清一色碰碰胡
                'peng' => [],
                'gang' => [],//组合类型：[3,3,3,3,2]
                'cards' => [1, 1, 1, 2, 2, 2, 4, 4, 4, 6, 6, 6, 7, 7],
            ],
            5 => [
                'color' => 2, //清一色金钩钓
                'peng' => [1, 2, 4, 6],
                'gang' => [],//组合类型：[3,3,3,3,2]
                'cards' => [7, 7],
            ],
            6 => [
                'color' => 3,//将对
                'peng' => [2, 5, 8, 12],
                'gang' => [],//组合类型：[3,3,3,3,2]
                'cards' => [15, 15],
            ],
            7 => [
                'color' => 3,//带幺九
                'peng' => [],
                'gang' => [],//组合类型：[1,1,1,1,1,3,3,3]
                'cards' => [1, 2, 3, 7, 8, 9, 11, 11, 11, 19, 19, 19, 9, 9],
            ],
            8 => [
                'color' => 2,//清一色带幺九
                'peng' => [],
                'gang' => [],//组合类型：[3,1,1,3,3,3]
                'cards' => [1, 1, 1, 2, 3, 7, 7, 7, 8, 8, 8, 9, 9, 9],
            ],
            9 => [
                'color' => 2,//十八罗汉
                'peng' => [],
                'gang' => [1, 2, 3],//组合类型：[1,1,3,3,3,3]
                'cards' => [4, 4, 4, 4, 8, 8],
            ],
            10 => [
                'color' => 1,//平胡
                'peng' => [11, 18],
                'gang' => [],//组合类型：[3,1,1,1,1,2,3,3]
                'cards' => [12, 13, 14, 15, 15, 23, 23, 23],
            ],
            11 => [
                'color' => 2,//平胡
                'peng' => [],
                'gang' => [], //组合类型：[3,2,2,1,3,3]
                'cards' => [2, 2, 2, 3, 3, 4, 4, 5, 25, 25, 25, 29, 29, 29],
            ],
            12 => [
                'color' => 1,//平胡
                'peng' => [],
                'gang' => [],  //组合类型：[3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
                'cards' => [11, 11, 11, 13, 13, 14, 15, 16, 17, 18, 19, 22, 23, 24],
            ],
            13 => [
                'color' => 3,//平胡
                'peng' => [],
                'gang' => [],  //组合类型：[3, 3, 1, 2, 1, 1, 1, 1, 1]
                'cards' => [2, 2, 2, 5, 5, 5, 6, 7, 7, 8, 9, 15, 16, 17],
            ],
            14 => [
                'color' => 2,//平胡
                'peng' => [],
                'gang' => [],  //组合类型：[1, 1, 1, 2, 1, 1, 1, 2, 2, 2]
                'cards' => [4, 5, 6, 22, 22, 23, 24, 25, 26, 26, 27, 27, 28, 28],
            ],
            15 => [
                'color' => 3,//平胡
                'peng' => [],
                'gang' => [],  //组合类型：[1, 1, 1, 1, 1, 1, 3, 1, 1, 3]
                'cards' => [1, 2, 3, 4, 5, 6, 7, 7, 7, 8, 9, 16, 16, 16],
            ],
            16 => [
                'color' => 2,//平胡
                'peng' => [],
                'gang' => [],  //组合类型：[1, 1, 1, 1, 1, 1, 3, 1, 1, 3]
                'cards' => [1, 1, 1, 2, 3, 4, 4, 4, 5, 5, 5, 7, 7, 7],
            ],
            17 => [
                'color' => 2,//平胡
                'peng' => [1, 5, 7],
                'gang' => [],  //组合类型：[3, 1, 1, 3, 3, 3]
                'cards' => [2, 3, 4, 4, 4],
            ],
            18 => [
                'color' => 1,//平胡
                'peng' => [],
                'gang' => [],  //组合类型：[1, 1, 1, 1, 1, 1, 3, 1, 1, 3]
                'cards' => [12, 13, 14, 15, 16, 17, 23, 24, 25, 26, 26, 26, 27, 28],
            ],
            19 => [
                'color' => 3,//平胡
                'peng' => [12],
                'gang' => [],  //组合类型：[1, 1, 1, 3, 1, 1, 3, 1, 1, 1]
                'cards' => [1, 2, 3, 5, 5, 5, 6, 7, 17, 18, 19],
            ],
            20 => [
                'color' => 3,//平胡
                'peng' => [],
                'gang' => [],  //组合类型：[2, 2, 2, 2, 1, 1, 1, 1, 1, 1]
                'cards' => [1, 1, 2, 2, 3, 3, 6, 6, 7, 8, 9, 16, 17, 18],
            ],
        ];
//        $index = 12;
        $cards = $arr[$index]['cards'];
        $colorNum = $arr[$index]['color'];
        $peng = $arr[$index]['peng'];
        $gang = $arr[$index]['gang'];
        //校验牌数，牌数小于14或者大于18，则必定不能胡牌

        $checkNum = PaiXing::checkNum($cards);
        if (!$checkNum) {
            return ERROR('胡牌失败，当前手牌数量有误！');
        }
        //校验定缺，没有定缺花色的牌才可以胡牌
        $checkColor = PaiXing::checkColor($cards, $colorNum);
        if (!$checkColor) {
            return ERROR('胡牌失败，当前手牌中有定缺牌！');
        }
        //计算胡牌
        $hu = (new Hupai())->start($cards, $peng, $gang);
        dd('hu', $hu);
        if (empty($hu)) {
            return ERROR('炸胡');
        }
        //校验牌型
        $result = PaiXing::checkPaiXing($hu, $peng, $gang);
        if (!is_array($result)) {
            return ERROR($result);
        }
        dd('result', $result);
        return SUCC($result);
    }


    public function testhu(RequestInterface $request)
    {
        $color = intval($request->input('color'));
        $peng = $request->input('peng', '');
        $gang = $request->input('gang', '');
        $card = $request->input('card', '');

        $cards = $card ? explode(',', $card) : [];
        $gang = $gang ? explode(',', $gang) : [];
        $peng = $peng ? explode(',', $peng) : [];
        dd('cards', $cards);
        dd('gang', $gang);
        dd('peng', $peng);
        dd('color', $color);
        //校验牌数，牌数小于14或者大于18，则必定不能胡牌

        $checkNum = PaiXing::checkNum($cards, $peng, $gang);
        if (!$checkNum) {
            return ERROR('胡牌失败，当前手牌数量有误！');
        }
        //校验定缺，没有定缺花色的牌才可以胡牌
        $checkColor = PaiXing::checkColor($cards, $color);
        if (!$checkColor) {
            return ERROR('胡牌失败，当前手牌中有定缺牌！');
        }
        //计算胡牌
        $hu = (new Hupai())->start($cards, $peng, $gang);
        dd('hu', $hu);
        if (empty($hu)) {
            return ERROR('炸胡');
        }
        //校验牌型
        $result = PaiXing::checkPaiXing($hu, $peng, $gang);
        if (!is_array($result)) {
            return ERROR($result);
        }
        return SUCC($result);

    }

    public function testcacular()
    {
        $room_data = config('simulate_data');
        $uinfo_arr = $room_data['uinfo'];
        $returnData = [];
        $chairs = [5 => 1, 6 => 2, 7 => 3, 8 => 4];
        foreach ($chairs as $account => $chair_id) {
            // {"chair_id":1,"score":64,"nickname":"GGYY","total_bonus":16,"detial_list":[{"reward":"清一色","bonus":8},......]}
            $userData = $room_data[$account];
            $total_bonus = 0;
            foreach ($userData['hu_detail'] as $v) {
                $total_bonus += $v['bonus'];
            }
            $userInfo = \App\Model\User::userInfo($account);
            $returnData[] = [
                'chair_id' => $chair_id,
                'account' => $account,
                'score' => $uinfo_arr[$account]['score'],
                'nickname' => $userInfo['nickname'],
                'total_bonus' => $total_bonus,
                'detial_list' => $userData['hu_detail'],
                'is_robot' => $userData['is_robot']
            ];
        }

        //更新用户积分并记录流水
        updateScore($returnData);

//        dd('gameOver returnData', $returnData);
        return $returnData;
    }
}
